#include <iostream>
#include <vector>
using namespace std;

int isPrime(int num){
    if(num<2)    return false;
    for(int i=2;i*i<=num;i++){
        if(num%i==0)    return false;
    }
    return true;
}

int rever(int num,int d){
    vector<int> v;
    while(num){
        v.push_back(num%d);
        num /= d;
    }
    int res = 0;
    for(int i=i;i<v.size();i++){
        res = res*d+v[i];
    }
    return res;
}

signed main()
{
    int a,b;
    while(cin>>a){
        if(a>0){
            cin>>b;
            if(isPrime(a)){
                if(isPrime(rever(a,b)))    cout<<"Yes\n";
                else    cout<<"No\n";
            }
            else    cout<<"No\n";
        }
        else    break;
    }
    return 0;
}
